import { acceptHMRUpdate, defineStore } from 'pinia'
import { list, RInform } from "/@/api/reviewH5/index"
import { ref } from 'vue'
import { userInfoStrore } from "/@/stores/userInfoStore"
import { sm2 } from 'sm-crypto'
import { log } from 'console'
import { useRouter } from "vue-router";

const router = useRouter();

const store = userInfoStrore()
let userInfo = store.userInfoObj as any
export const policyListStore = defineStore('policyList', () => {
    const router = useRouter();
    const policyListArr = ref({})
    const noDataUrl = ref("")
    const taskNo = ref("")
    const inletURL = ref("") // 入口页面地址
    const taskType = ref("") // 1 是生成， 2 是查看
    const reviewTipsUri = ref("") // 温馨提示页面
    const letterAdvceSL = ref("") //告知信页面的sl
    const setTaskNo = (no: any) => {
        taskNo.value = no
    }
    const setInletURL = (url: any) => {
        inletURL.value = url
        localStorage.setItem('inletURL', url)
    }

    const setLetterAdvceSL = (data: any) => {
        letterAdvceSL.value = data
        localStorage.setItem('letterAdvceSL', data)
    }
    const setTaskType = (type: any) => {
        taskType.value = type
        localStorage.setItem('taskType', type)
    }
    const setReviewTipsUri = (url: any) => {
        reviewTipsUri.value = url
        localStorage.setItem('reviewTipsUri', url)
    }
    //私钥串---解密使用
    const privateKey = ref<string>(
        '5aa03412c3051e1d4cf9d19cfbeeec70c28f388c9f82747cc912096c9cd44bea'
    )
    // 解密方法
    const JIMIPublicData = (data: any): string => {
        const cipherMode = 1
        //data为加密的数据
        const result = sm2.doDecrypt(
            data.substring(2),
            privateKey.value,
            cipherMode
        )
        return result
    }
    const getPolicyList = async (taskNo: any,queryScope:any): Promise<any> => {
        // taskNo?
        //如果从详情页回来，没有三要素，先请求三要素，在请求列表
        // 根据路由里带的toskcode获取三要素
        const isJstLogin = sessionStorage.getItem('jstJointLoginVo')
        var a = JSON.parse(isJstLogin)
        let obj = { ...store.userInfoObj }
        if(isJstLogin) {
            obj = {...a,taskNo: taskNo,taskCode: taskNo,auth:"FACE",queryScope:queryScope?queryScope:0}
        } else {
            obj = { ...store.userInfoObj,queryScope:queryScope?queryScope:0 }
        }
        let newobj = {}
        if (obj?.documentName && obj.documentType && obj.documentNo) {
            newobj = {
                taskNo: taskNo,
                ...obj
            }
            
            let params: any = JSON.stringify(newobj)
            return new Promise((resolve, reject) => {
                list(params).then((res: any) => {
                    if (res.code == 200) {
                        // 250415 因追加看自己筛选，判断条件 导致筛选出来的保单可能为0，但实际有保单，不跳转到无保单页面
                        // if (res.data?.insuranceList?.length > 0) {
                        if (res.data?.count?.insuranceCount > 0) {
                            res.data?.insuranceList.map((item: any) => {
                                item.checked = false
                                item.openOrClose = 'open'
                            })
                        } else {
                            noDataUrl.value = res.data.url.replace("%taskNo", taskNo)
                        }
                        policyListArr.value = res.data
                        resolve(policyListArr.value)
                    } else if (res.code == 402) {
                        router.push({ path: 'reportingResultwxts' })
                    }
                    else {
                        reject(res.msg)
                    }
                })
            })
        } else {
            //没有三要素
            const res: any = await RInform(taskNo)
            if (res.code == 200) {
                //保存的是加密的用户信息
                const userInfoObj = ref({
                    documentName: res.data.documentName,
                    documentNo: res.data.documentNo,
                    documentType: res.data.documentType,
                    mobileNo: res.data.mobileNo,
                    auth: res.data.auth,
                    nexturl: res.data.url,
                    taskCode: taskNo,
                })
                store.postUserInfo(userInfoObj.value)
                //保存的是明文的用户信息，用户页面展示文字
                const postproWritingUsetInfo = ref({
                    documentName: JIMIPublicData(res.data.documentName as string),
                    documentNo: res.data.documentNo
                        ? JIMIPublicData(res.data.documentNo as string)
                        : res.data.documentNo,
                    documentType: res.data.documentType,
                    mobileNo: res.data.mobileNo,
                    auth: res.data.auth,
                    nexturl: res.data.url,
                    taskCode: taskNo,
                })
                // 去掉免密登录功能
                // 当前是拿事务id,现在给后端
                store.postProWritingUsetInfo(postproWritingUsetInfo.value)
            }
            let newobj = {
                taskNo: taskNo,
                ...store.userInfoObj,
            }
            let params: any = JSON.stringify(newobj)
            return new Promise((resolve, reject) => {
                list(params).then((res: any) => {
                    if (res.code == 200) {
                        if (res.data?.insuranceList?.length > 0) {
                            res.data?.insuranceList.map((item: any) => {
                                item.checked = false
                                item.openOrClose = 'open'
                            })
                        } else {
                            noDataUrl.value = res.data.url.replace("%taskNo", taskNo)
                        }
                        policyListArr.value = res.data
                        resolve(policyListArr.value)
                    } else if (res.code == 402) {
                        router.push({ path: 'reportingResultwxts' })
                    } else {
                        reject(res.msg)
                    }
                })
            })

        }

    }
    return { policyListArr, taskNo, inletURL, taskType, noDataUrl, reviewTipsUri, letterAdvceSL, setLetterAdvceSL, getPolicyList, setTaskNo, setInletURL, setTaskType, setReviewTipsUri }
}, {
    persist: true
})

if (import.meta.hot) {
    import.meta.hot.accept(acceptHMRUpdate(policyListStore, import.meta.hot))
}  